#ifndef MAINHANDLER_H
#define MAINHANDLER_H




class MainHandler : public Handler 
{
public:
	MainHandler(MainView * mainView, Model * model)
		:mainView(mainView), model (model){}
	~MainHandler(){}

public:
	int  create() 
	{
		/*
		MessageBox(
			NULL,
			(LPCWSTR)L"Resource not available\nDo you want to try again?",
			(LPCWSTR)L"Account Details",
			MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2
			);
		*/
		return 0;
		
	}
	int keyUp(int key, LPARAM lParam)
	{
		/*
		this->n_wParam = key ;
		this->n_lParam = lParam ;
		::EnumChildWindows(handle, (WNDENUMPROC)enumerateChildren, (LPARAM)WM_KEYUP);
		*/

		//simply set focus on the window is OK
		
		

		//Broadcast message to all the child windows. The WM_KEYUP messages cannot 
		// be received directly by child windows. 
		//::EnumChildWindows(handle, (WNDENUMPROC)enumerateChildren, (LPARAM)WM_KEYUP);     
		//So we switch the mode here

		return 0;
	}
	int command(int id, int cmd, LPARAM msg)
	{
		//WCHAR * pszPath;
		CONTROL::FileOpenDialog fileOpenDlg ;
		switch(id)
		{
		case ID_FILE_EXIT:
			::PostMessage(handle, WM_CLOSE, 0, 0);
			break;

		case ID_FILE_OPEN_M:
			// create a modal dialog box, and destroy it in aboutDialogProcedure().
			//::DialogBox((HINSTANCE)::GetWindowLongPtr(handle, GWL_HINSTANCE), MAKEINTRESOURCE(IDD_ABOUT), handle, aboutDialogProcedure);
			fileOpenDlg.set(handle);
			fileOpenDlg.Invoke();
			
			break;

		case ID_RENDER_FLAT :
			model->setShadeMode(FLAT);
			model->setUpdated(model->cullingView, true );
			break;

		case ID_RENDER_SMOOTH :
			model->setShadeMode(SMOOTH);
			model->setUpdated(model->cullingView, true );
			break; 
		case ID_VIEW_FILL:
			model->setViewMode(FILL_MODE);
			model->setUpdated(model->cullingView, true );
			break;
		case ID_VIEW_FRAME:
			model->setViewMode(FRAME_MODE);
			model->setUpdated(model->cullingView, true );
			break;
		case ID_VIEW_POINT:
			model->setViewMode(POINT_MODE);
			model->setUpdated(model->cullingView, true );
			break;
		}

		return 0;
	}
    int size(int w, int h, WPARAM wParam)
	{
		::EnumChildWindows(handle, (WNDENUMPROC)enumerateChildren, (LPARAM)WM_PAINT);
		::EnumChildWindows(handle, (WNDENUMPROC)enumerateChildren, (LPARAM)WM_SIZE);
		UpdateWindow(handle);
		return 0;
	}

	static bool CALLBACK enumerateChildren(HWND handle, LPARAM lParam)
	{
		wchar_t windowname[11] ;
		switch (lParam)
		{
		case WM_CLOSE :
			::SendMessage (handle, WM_CLOSE, 0, 0); 
			break ;
		case WM_PAINT:
			::SendMessage (handle, WM_PAINT, 0, 0);
			break;
		case WM_KEYUP :
			//::SendMessage (handle, WM_KEYUP, n_wParam, n_lParam);
			break;
		case WM_SIZE :
			HWND parent = ::GetParent(handle);
			RECT rect ;
			::GetWindowRect(parent, & rect);
			
			::GetWindowText(handle, windowname, 10);
			if (windowname[0] == 'L')
			{
				//::SendMessage (handle, WM_SIZE, (rect.right - rect.left) * .5f, rect.bottom - rect.top);
				//::SetWindowPos(handle, 0, 0, 0 , (rect.right - rect.left) * .5f , rect.bottom - rect.top, SWP_NOZORDER);
			}
			else if (windowname[0] == 'C')
			{
				//::SendMessage (handle, WM_SIZE, (rect.right - rect.left) * .3f, rect.bottom - rect.top);
				//::SetWindowPos(handle, 0, (rect.right - rect.left) * .5f , 0 , (rect.right - rect.left) * .3f , rect.bottom - rect.top, SWP_NOZORDER);
			}
			else
			{
				//::SendMessage (handle, WM_SIZE, (rect.right - rect.left) * .2, rect.bottom - rect.top);
			}
			break; 
		}
		if(lParam == WM_CLOSE)
		{
			::SendMessage(handle, WM_CLOSE, 0, 0);  // close child windows
		}

		return true;
	}

private:
	MainView * mainView; 
	Model * model ;
	//static int vk_value; 
	//static WPARAM n_wParam ;
	//static LPARAM n_lParam ;
};

#endif
